<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
List processing
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial029.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial031.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc64">4.7</A>&nbsp;&nbsp;List processing</H2>

Lists are probably the most heavily used data structure in Prolog and
ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP>. Apart from unification/matching, the most commonly used
list processing predicates are: append/3, length/2, member/2 and sort/2.
The append/3 predicate can be used to append lists or to split lists:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- append([1, 2], [3, 4], L).
L = [1, 2, 3, 4]
Yes (0.00s cpu)
?- append(A, [3, 4], [1, 2, 3, 4]).
A = [1, 2]
More (0.00s cpu)
No (0.01s cpu)
?- append([1, 2], B, [1, 2, 3, 4]).
B = [3, 4]
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
The length/2 predicate can be used to compute the length of a list
or to construct a list of a given length:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- length([1, 2, 3, 4], N).
N = 4
Yes (0.00s cpu)
?- length(List, 4).
List = [_1693, _1695, _1697, _1699]
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
The member/2 predicate can be used to check membership in a list
(but memberchk/2 should be preferred for that purpose),
or to backtrack over all list members:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- memberchk(2, [1, 2, 3]).
Yes (0.00s cpu)
?- member(X, [1, 2, 3]).
X = 1
More (0.00s cpu)
X = 2
More (0.01s cpu)
X = 3
Yes (0.01s cpu)
</PRE></BLOCKQUOTE>
The sort/2 predicate can sort any list and remove duplicates:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- sort([5, 3, 4, 3, 2], Sorted).
Sorted = [2, 3, 4, 5]
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>

<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>&#8857;</B><DD CLASS="dd-description"> <FONT COLOR="#9832CC">For more list processing utilities, see the documentation for library(lists).</FONT>
</DL>

<HR>
<A HREF="tutorial029.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial031.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
